/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.atomojo.app.util;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import org.atomojo.app.App;
import org.atomojo.app.AppException;
import org.atomojo.app.auth.User;
import org.atomojo.app.client.XMLRepresentationParser;
import org.atomojo.app.db.Entry;
import org.atomojo.app.db.EntryMedia;
import org.atomojo.app.db.Feed;
import org.infoset.xml.Document;
import org.infoset.xml.XMLException;
import org.restlet.representation.Representation;

/**
 *
 * @author alex
 */
public class CopyFeedTask implements Runnable {

   App app;
   User user;
   Feed sourceRoot;
   Feed targetParent;
   String targetName;
   Set<String> errors;
   
   public CopyFeedTask(App app,User user,Feed source,Feed targetParent,String targetName)
   {
      this.app = app;
      this.user = user;
      this.sourceRoot = source;
      this.targetParent = targetParent;
      this.targetName = targetName;
      this.errors = new TreeSet<String>();
   }
   
   public void run() {
      copy(sourceRoot,targetParent,targetName);
   }
   
   protected void copy(Feed source,Feed parent,String name)
   {
      XMLRepresentationParser parser = new XMLRepresentationParser();
      try {
         // get the feed document without the entries
         Document feedDoc = parser.load(app.getStorage().getFeedHead(source.getPath(), source.getUUID()));

         // Create the feed.  The ID will be changed since the feed exits
         Feed newFeed = app.createFeed(parent.getPath()+name, feedDoc);
         
         // copy the entries
         Iterator<Entry> entries = source.getEntries();
         while (entries.hasNext()) {
            Entry entry = entries.next();
            Document entryDoc = parser.load(app.getEntryRepresentation("", entry));
            Iterator<EntryMedia> mediaResources = entry.getResources();
            if (mediaResources.hasNext()) {
               // we have a media entry and so copy the media
               EntryMedia media = mediaResources.next();
               Representation mediaRep = app.getStorage().getMedia(source.getPath(), source.getUUID(), media.getName());
               Entry newEntry = app.createMediaEntry(user,newFeed,mediaRep,media.getName(),UUID.randomUUID());
               app.updateEntry(user,newFeed,newEntry,entryDoc);
               // TODO: copy multiple media ?
            } else {
               // Just copy the entry.  The ID will be changed since the entry exists
               app.createEntry(user, newFeed, entryDoc);
            }
         }
         
         // copy the sub feeds
         Iterator<Feed> children = source.getChildren();
         while (children.hasNext()) {
            Feed child = children.next();
            copy(child,newFeed,child.getName());
         }
      } catch (SQLException ex) {
         errors.add(ex.getMessage());
      } catch (IOException ex) {
         errors.add(ex.getMessage());
      } catch (XMLException ex) {
         errors.add(ex.getMessage());
      } catch (AppException ex) {
         errors.add(ex.getMessage());
      }         
   }
   
   public Set<String> getErrors() {
      return errors;
   }
}
